package com.foreverwin.jzymes.framework.quartz.controller;

import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.annotation.ParamBind;
import com.foreverwin.jzymes.framework.base.AjaxResult;
import com.foreverwin.jzymes.framework.exception.BusinessException;
import com.foreverwin.jzymes.framework.quartz.domain.SysJob;
import com.foreverwin.jzymes.framework.quartz.domain.SysJobParam;
import com.foreverwin.jzymes.framework.quartz.service.IScheduleAdviceService;
import com.foreverwin.jzymes.framework.quartz.service.ISysJobParamService;
import com.foreverwin.jzymes.framework.quartz.service.ISysJobService;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/quartz")
public class ScheduleController {

    @Autowired
    IScheduleAdviceService scheduleAdviceService;
    @Autowired
    ISysJobService sysJobService;
    @Autowired
    ISysJobParamService sysJobParamService;

    @RequestMapping("/jobMaintain")
    @ParamBind
    public AjaxResult maintainJob(SysJob job, ArrayList<SysJobParam> jobParamList ){
        try{
            String site = CommonMethods.getSite();
            job.setSite( site );
            Assert.hasText( job.getSite(), "common.siteNotBlank" );

            if(StringUtils.isBlank( job.getHandle() ) ){
                scheduleAdviceService.createJob( job, jobParamList );
            }else{
                scheduleAdviceService.updateJob( job, jobParamList );
            }
            return AjaxResult.returnSuccess();
        }catch ( BusinessException e ){
            return AjaxResult.returnError( e.getMessage() );
        }
    }

    @RequestMapping("/jobList")
    public AjaxResult findSysJobList(){
        String site = CommonMethods.getSite();
        Assert.hasText( site, "common.siteNotBlank" );

        SysJob queryParam = new SysJob();
        queryParam.setSite( site );
        List<SysJob> jobList = sysJobService.selectSysJobList( queryParam );

        List<Map<String,String>> rtnJobList = new ArrayList<>();
        for( SysJob job : jobList ){
            Map<String,String> jobMap = new HashMap<>();
            jobMap.put( "handle", job.getHandle() );
            jobMap.put( "jobName", job.getJobName() );
            jobMap.put( "methodName", job.getMethodName() );
            jobMap.put( "jobDesc", job.getJobDesc() );
            rtnJobList.add( jobMap );
        }

        return AjaxResult.returnSuccess().put( "value", rtnJobList );
    }

    @RequestMapping( "/job" )
    public AjaxResult findSySJob( @RequestParam String jobId ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, "common.siteNotBlank" );
        Assert.hasText( jobId, I18nUtil.getI18nText( "system.quartz.jobNotBlank" ));

        SysJob job = sysJobService.selectSysJobById( jobId );
        Assert.notNull( job, I18nUtil.getI18nText( "system.quartz.jobNotExist", new String[]{ jobId } ));

        SysJobParam queryParam = new SysJobParam();
        queryParam.setJobBo( job.getHandle() );
        List<SysJobParam> jobParamList = sysJobParamService.selectSysJobParamList( queryParam );

        Map<String,Object> rtnMap = new HashMap<>();
        rtnMap.put( "handle", job.getHandle() );
        rtnMap.put( "site", job.getSite() );
        rtnMap.put( "jobName", job.getJobName() );
        rtnMap.put( "jobDesc", job.getJobDesc() );
        rtnMap.put( "methodName", job.getMethodName() );
        rtnMap.put( "cronExpression", job.getCronExpression() );
        rtnMap.put( "misfirePolicy", job.getMisfirePolicy() );
        rtnMap.put( "status", job.getStatus() );
        rtnMap.put( "parameterList", jobParamList );

        return AjaxResult.returnSuccess().put( "value", rtnMap );
    }
}
